<template>
  <tl-sub-page :render="render" ref="drawerRef" :visible.sync="visible" :with-header="false" :modal-append-to-body="true" :close-on-press-escape="false" :append-to-body="true" class="tl-drawer" :size="'95%'" @open="open" @close="close" :destroy-on-close="true">
    <div class="mpm-project-container">
      <div style="width:0px;overflow:hidden;height:0px;"><input type="text" /></div>
      <div class="mpm-project-header">
        <div class="info">
          <img src="/static/images/paojectManager/mpm_supervise_round.png" class="logo" style="width: 55px;height: 55px" />
          <div class="content" :title="model.projectname">
            <p class="displaychange" v-show="model.projectname">
              <b>{{ model.projectname + '-' }}</b>
              <b>项目督办</b>
            </p>
            <p class="light" v-show="model.projectmanager">
              <span>项目经理：{{ model.projectmanager + '【' + model.projectmanagercode + '】' }}</span>
              <span>创建时间：{{ formatDate(model.createon) }}</span>
            </p>
          </div>
        </div>
        <div class="toolbar">
          <el-button type="danger" size="small" icon="el-icon-close" circle @click="doCancelEdit"></el-button>
        </div>
      </div>
      <div class="mpm-project-main" :style="{ height: mainHeight + 'px' }">
        <el-scrollbar class="mpm-project-main-container" id="mpmMainContainer">
          <div style="padding:0 10px">
            <el-form class="tl-form" style="flex:1" ref="form" :model="model" :rules="rules" label-width="150px">
              <div class="tl-form-line-title">
                基本信息
              </div>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="督办单名称" prop="supervisename" :rules="rules.validateRequire">
                    <el-input v-model="model.supervisename"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="督办单编号" prop="supervisecode">
                    <el-input v-model="model.supervisecode" :disabled="true" placeholder="系统自动赋值"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="督办对象类型" prop="superviseobjecttype" :rules="rules.validateRequire">
                    <el-select v-model="model.superviseobjecttype" filterable clearable @change="changesuperviseobjecttype" :disabled="true">
                      <el-option v-for="item in codeModel.superviseobjecttypeList" :key="item.code" :label="item.name" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="督办对象" prop="superviseobject" :rules="rules.validateRequire" v-show="milepostvisible">
                    <el-input v-model="model.superviseobject" :disabled="true" class="tl-ref-selector" :class="{ disabled: dodisabled }">
                      <el-button slot="append" type="primary" icon="el-icon-search" @click="openSdProjectMilepostDialog" :disabled="dodisabled"></el-button>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="督办对象" prop="superviseobject" :rules="rules.validateRequire" v-show="projectVisible">
                    <el-input v-model="model.superviseobject" :disabled="true" class="tl-ref-selector" :class="{ disabled: dodisabled }"> </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="督办对象" prop="superviseobject" :rules="rules.validateRequire" v-show="taskvisible">
                    <el-select v-model="model.superviseobject" filterable clearable :disabled="dodisabled">
                      <el-option v-for="item in codeModel.superviseobjectList" :key="item.code" :label="item.name" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="责任人" prop="responsiblecode" :rules="rules.validateRequire">
                    <tl-ref-user v-model="model.responsiblecode" :initDisplayData.sync="model.responsiblename" dataType="code" :singleSelect="true" @confirm="userConfirm"></tl-ref-user>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="督办期限" prop="superviseperiod">
                    <el-date-picker v-model="model.superviseperiod" type="date" placeholder="督办期限" value-format="yyyy-MM-dd"></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="督办原因" prop="supervisereason" :rules="rules.validateRequire">
                    <el-input v-model="model.supervisereason" type="textarea" :rows="4"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="督办部门" prop="supervisedeptname">
                    <el-input v-model="model.supervisedeptname" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="督办发起人" prop="supervisepeople">
                    <el-input v-model="model.supervisepeople" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="督办状态" prop="supervisestate">
                    <el-select v-model="model.supervisestate" filterable clearable :disabled="true">
                      <el-option v-for="item in codeModel.supervisestateList" :key="item.code" :label="item.name" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="抄送人员范围" prop="ccstaffname">
                    <tl-ref-user v-model="model.ccstaffcode" :initDisplayData.sync="model.ccstaffname" dataType="code" :singleSelect="false" @confirm="staffConfirm"></tl-ref-user>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="备注" prop="description">
                    <el-input v-model="model.description" type="textarea" :rows="4"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <div class="tl-form-line-title">
                【单据】申请信息
              </div>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请部门" prop="applydeptname" :rules="rules.validateRequire">
                    <tl-ref-dept v-model="model.applydeptcode" :initDisplayData.sync="model.applydeptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{ qry_enabled: 1 }"></tl-ref-dept>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请公司" prop="applyorgname">
                    <el-input v-model="model.applyorgname" :readonly="true" class="is-disabled"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请人姓名" prop="applyusername" :rules="rules.validateRequire">
                    <tl-ref-user v-model="model.applyusername" :initDisplayData.sync="model.applyusername" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人工号" prop="applyusercode">
                    <el-input v-model="model.applyusercode" :readonly="true" class="is-disabled"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="单据日期" prop="docdate">
                    <el-date-picker v-model="model.docdate" type="date" :disabled="true" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="float: right; margin-top: 10px">
              <tl-attachment ref="attachmentRef" sourceType="GpmProjectSupervise" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit">
                <el-button type="primary" :disabled="disableAttachment" icon="el-icon-paperclip" @click="$refs.attachmentRef.openUploadDialog()" size="small"> {{ this.$refs.attachmentRef ? this.$refs.attachmentRef.btnTitle : '' }} [{{ this.$refs.attachmentRef ? (this.$refs.attachmentRef.fileList.length > 0 ? this.$refs.attachmentRef.fileList.length : this.$refs.attachmentRef.fileCount) : 0 }}] </el-button>
              </tl-attachment>
              <tl-attachment ref="attachmentResponseRef" sourceType="GpmProjectSuperviseResponse" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit && model.state !== 0 && model.state !== 2" :btnTitle="'反馈附件'">
                <el-button type="primary" :disabled="disableAttachment" icon="el-icon-paperclip" @click="$refs.attachmentResponseRef.openUploadDialog()" size="small"> {{ this.$refs.attachmentResponseRef ? this.$refs.attachmentResponseRef.btnTitle : '' }} [{{ this.$refs.attachmentResponseRef ? (this.$refs.attachmentResponseRef.fileList.length > 0 ? this.$refs.attachmentResponseRef.fileList.length : this.$refs.attachmentResponseRef.fileCount) : 0 }}] </el-button>
              </tl-attachment>
              <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory" size="small"> <svg-icon icon-class="list" />审批历史 </el-button>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <tl-approval-history ref="history" sourceType="GpmProjectSupervise" :sourceId="model.id" />
      <tlsdProject-milepost-ref ref="sdProjectMilepostRef" @confirm="getSdProjectMilepostData" :isSingleSelect="true" :externalQueryParam="externalQueryParamForMilepost"></tlsdProject-milepost-ref>
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlsdProjectMilepostRef from '@/views/gpm/gpmProjectMilepost/refList'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { formatDate } from '@tapui/utils/date'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlsdProjectMilepostRef },
  data() {
    return {
      moduleTitle: 'SD项目督办-PMO审批',
      urlPrefix: '/sd/projectSupervise/',
      url: {
        // 额外的url
        updateSuperviseUser: '/sd/projectSupervise/updateSuperviseUser'
      },
      mainHeight: 1000,
      fromApproval: '',
      milepostvisible: false,
      taskvisible: true,
      projectVisible: false,
      externalQueryParamForMilepost: {}
    }
  },
  props: {
    render: {
      default: 'el-drawer',
      type: String
    },
    projectinfo: {}
  },
  computed: {
    dodisabled() {
      return (this.isAdd === false && this.disableEditSubmit === true) || this.fromApproval === '1'
    }
  },
  methods: {
    // 选择责任人
    userConfirm(selectionRow) {
      this.$set(this.model, 'responsiblecode', selectionRow[0].code)
      this.$set(this.model, 'responsiblename', selectionRow[0].name)
    },
    // 选择抄送人员范围
    staffConfirm(selectionRow) {
      if (!Array.isArray(selectionRow)) {
        this.$set(this.model, 'ccstaffcode', selectionRow[0].code)
        this.$set(this.model, 'ccstaffname', selectionRow[0].name)
      } else {
        let tempname = ''
        let tempcode = ''
        selectionRow.forEach((item, index) => {
          this.$set(this.model, 'ccstaffcode', item.code)
          this.$set(this.model, 'ccstaffname', item.name)
          if (tempname.length > 0) {
            tempname = tempname + '，' + item.name
            tempcode = tempcode + ',' + item.code
          } else {
            tempname = item.name
            tempcode = item.code
          }
        })
        this.$set(this.model, 'ccstaffcode', tempcode)
        this.$set(this.model, 'ccstaffname', tempname)
      }
    },
    /** 打开里程碑参照 */
    openSdProjectMilepostDialog() {
      if (this.model.superviseobjecttype === '' || this.model.superviseobjecttype === undefined) {
        this.$tap.alert('请先选择督办对象类型', 'warning')
        return
      }
      const { projectcode } = this.projectinfo
      this.externalQueryParamForMilepost = {
        qry_projectcode: projectcode,
        qry_enabled: 1,
        qry_milepoststate: 0
      }
      this.$refs.sdProjectMilepostRef.visible = true
    },
    /** 获取里程碑参照数据 */
    getSdProjectMilepostData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'superviseobject', selectionRow.milepostname)
      this.$set(this.model, 'superviseobjectcode', selectionRow.milepostcode)
      // 里程碑赋值责任人
      this.$set(this.model, 'responsiblecode', selectionRow.responsiblecode)
      this.$set(this.model, 'responsiblename', selectionRow.responsible)
    },
    // 督办对象类型
    changesuperviseobjecttype(e) {
      // 清除历史数据
      this.$set(this.model, 'superviseobject', '')
      this.$set(this.model, 'superviseobjectcode', '')
      this.$set(this.model, 'responsiblecode', '')
      this.$set(this.model, 'responsiblename', '')
      // 其他
      if (e === '0') {
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = true
        this.$set(this.model, 'superviseobjectcode', this.projectinfo.projectcode)
      } else if (e === '2') {
        // 里程碑
        this.projectVisible = false
        this.milepostvisible = true
        this.taskvisible = false
      } else {
        // 项目
        this.model.superviseobject = this.model.projectname
        this.milepostvisible = false
        this.taskvisible = false
        this.projectVisible = true
      }
    },
    /** 抽屉组件必有 **/
    resizeHandler() {
      this.mainHeight = document.body.getBoundingClientRect().height - 100 - (this.isApproval ? 110 : 0)
    },
    formatDate(date) {
      return formatDate(new Date(date), 'yyyy-MM-dd')
    },
    // 办理通过前保存修改的信息
    saveInfo(resolve, reject) {
      // 修改督办审批人员信息
      this.$set(this.model, 'applyusercode', this.user.code)
      this.$set(this.model, 'applyusername', this.user.name)
      this.$set(this.model, 'usercode', this.user.code)
      this.$set(this.model, 'username', this.user.name)
      this.$set(this.model, 'supervisepeoplecode', this.user.code)
      this.$set(this.model, 'supervisepeople', this.user.name)
      putAction(this.url.updateSuperviseUser, this.model)
        .then((res) => {
          if (res.success) {
            resolve()
          } else {
            this.$tap.alert(res.message, 'warning')
            reject()
          }
        })
        .catch((err) => {
          this.$tap.alert('保存失败，请联系系统管理员', 'warning')
          reject()
        })
    },
    loaded() {
      // 页面加载完毕执行
      this.resizeHandler()
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
      if (this.model.superviseobjecttype === '0') {
        this.milepostvisible = false
        this.taskvisible = true
        this.projectVisible = false
      } else if (this.model.superviseobjecttype === '2') {
        this.milepostvisible = true
        this.taskvisible = false
        this.projectVisible = false
      }
      if (this.stateApproval) {
        this.disableEditSubmit = true
      }
    },
    doCancelEdit() {
      this.visible = false
      this.$emit('close')
      this.$emit('refresh')
    }
  }
}
</script>
